using System;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace Net35.Xrm.Sdk.Query
{
	[DataContract(Name = "QueryExpression", Namespace = "http://schemas.microsoft.com/xrm/2011/Contracts")]
	public sealed class QueryExpression : QueryBase
	{
		private ColumnSet _columnSet;
		private string _entityName;
		private bool _distinct;
		private bool _noLock;
		private PagingInfo _pageInfo;
		private DataCollection<LinkEntity> _linkEntities;
		private FilterExpression _criteria;
		private DataCollection<OrderExpression> _orders;
		private int? _topCount;
		[DataMember]
		public bool Distinct
		{
			get
			{
				return this._distinct;
			}
			set
			{
				this._distinct = value;
			}
		}
		[DataMember(Order = 50)]
		public bool NoLock
		{
			get
			{
				return this._noLock;
			}
			set
			{
				this._noLock = value;
			}
		}
		[DataMember]
		public PagingInfo PageInfo
		{
			get
			{
				return this._pageInfo;
			}
			set
			{
				this._pageInfo = value;
			}
		}
		[DataMember]
		public DataCollection<LinkEntity> LinkEntities
		{
			get
			{
				if (this._linkEntities == null)
				{
					this._linkEntities = new DataCollection<LinkEntity>();
				}
				return this._linkEntities;
			}
			private set
			{
				this._linkEntities = value;
			}
		}
		[DataMember]
		public FilterExpression Criteria
		{
			get
			{
				return this._criteria;
			}
			set
			{
				this._criteria = value;
			}
		}
		[DataMember]
		public DataCollection<OrderExpression> Orders
		{
			get
			{
				if (this._orders == null)
				{
					this._orders = new DataCollection<OrderExpression>();
				}
				return this._orders;
			}
			private set
			{
				this._orders = value;
			}
		}
		[DataMember]
		public string EntityName
		{
			get
			{
				return this._entityName;
			}
			set
			{
				this._entityName = value;
			}
		}
		[DataMember]
		public ColumnSet ColumnSet
		{
			get
			{
				return this._columnSet;
			}
			set
			{
				this._columnSet = value;
			}
		}
		[DataMember(Order = 50, EmitDefaultValue = false)]
		public int? TopCount
		{
			get
			{
				return this._topCount;
			}
			set
			{
				this._topCount = value;
			}
		}
		public QueryExpression() : this(null)
		{
		}
		public QueryExpression(string entityName)
		{
			this._entityName = entityName;
			this._criteria = new FilterExpression();
			this._pageInfo = new PagingInfo();
			this._columnSet = new ColumnSet();
		}
		[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
		internal override void Accept(IQueryVisitor visitor)
		{
			visitor.Visit(this);
		}
		public void AddOrder(string attributeName, OrderType orderType)
		{
			this.Orders.Add(new OrderExpression(attributeName, orderType));
		}
		public LinkEntity AddLink(string linkToEntityName, string linkFromAttributeName, string linkToAttributeName)
		{
			return this.AddLink(linkToEntityName, linkFromAttributeName, linkToAttributeName, JoinOperator.Inner);
		}
		public LinkEntity AddLink(string linkToEntityName, string linkFromAttributeName, string linkToAttributeName, JoinOperator joinOperator)
		{
			LinkEntity linkEntity = new LinkEntity(this.EntityName, linkToEntityName, linkFromAttributeName, linkToAttributeName, joinOperator);
			this.LinkEntities.Add(linkEntity);
			return linkEntity;
		}
	}
}
